package com.simple.dsrouter.util;

import java.util.ArrayList;
import java.util.List;

import org.springframework.util.StringUtils;

public class ValidateUtils {
	

	public static List<String> validInterceptMethodPatterns(List<String> interceptMethodPatterns){
		if(interceptMethodPatterns == null){
			throw new IllegalArgumentException("interceptMethodPatterns arugment must not be null");
		}
		//spring's typical "xxx*", "*xxx" and "*xxx*" pattern styles, do not support "x**x"
		//仅仅支持上面的匹配格式
		List<String> compiledPattern = new ArrayList<String>(interceptMethodPatterns.size());
		for(String readMethodPattern : interceptMethodPatterns){
			if(StringUtils.countOccurrencesOf(readMethodPattern, "*") > 2){
				throw new IllegalArgumentException("interceptMethodPatterns only suppoer follows pattern style: \"xxx*\", \"*xxx\", \"*xxx*\" and \"xxx*yyy\"  must not be null");
			}
			int first = readMethodPattern.indexOf('*');
			int last = readMethodPattern.lastIndexOf('*');
			if(first >0 && last >0  && (first + 1) == last){
				throw new IllegalArgumentException("interceptMethodPatterns only suppoer follows pattern style: \"xxx*\", \"*xxx\", \"*xxx*\" and \"xxx*yyy\"  must not be null");
			}
			String tmp = readMethodPattern.trim();
			compiledPattern.add(tmp);
		}
		return compiledPattern;
	}

}